
//打开增加家族史 框
function addHealthFamilyPre(t){
	
	var memberId=$("#memberId").val();
	var healthFamilyId=$(t).attr("data-id");
	$.ajax({ 
		type : "get",
		dataType: "html",
		url : "../family/addHealthFamilyPre",
		async: true,
		data:{
			memberId:memberId,
			healthFamilyId:healthFamilyId
		},
		success : function(data) {
			$("#healthdialog").empty().html(data);
			deleteM(t,null);
		}
	});
}

//增加家族史 动作
function addHealthFamily(){
	var memberId=$("#memberId").val();
	var healthFamilyId;
	var relation=$("#relation").val();
	var diseaseName=$("#diseaseName").val();
	var remark=$("#remarkFamily").val();
	var sizeOfList=$("#sizeOfListFamily").val();
	if(!validateLength(relation,"关系",1,16)){
		return;
	}
	if(!validateLength(diseaseName,"疾病名称",1,16)){
		return;
	}
	
	
	if(!interceptIllegal(relation)){
		zhyAlert("关系含有非法字符");
		return;
	}
	if(!interceptIllegal(diseaseName)){
		zhyAlert("疾病含有非法字符");
		return;
	}
	if(memberId==null){
		zhyAlert("保存失败");
		return;
	}
	if(relation==''||relation==null){
		zhyAlert("请填写关系");
		return;
		
	}
	if(diseaseName==''||diseaseName==null){
		zhyAlert("请填写疾病名称");
		return;
		
	}
	if(remark){
		if($.trim(remark).length > 120){
			zhyAlert("【备注】应少于120个字符！");
			return;
		}
		if(!interceptIllegal(remark)){
			zhyAlert("【备注】含有非法字符");
			return;
		}
	}
	
	$.ajax({
		type : "post",
		dataType: "json",
		url : "../family/addHealthFamily",
		async: true,
		data:{
			memberId:memberId,
			diseaseName:diseaseName,
			relation:relation,
			remark:remark
		},
		success : function(data) {
			if(data.isSuccess){
				var heal_edit1 = data.heal_edit;//修改标示
				var heal_delete1 = data.heal_delete;//删除标示
				healthFamilyId=data.healthFamilyId;
				var healthHistoryId=data.healthHistoryId;
				sizeOfList=(eval(sizeOfList)+1);
				addRowFamily(sizeOfList,diseaseName,healthFamilyId,relation,remark,heal_edit1,heal_delete1);
				hidePop();
				
			}else{
				zhyAlert("保存失败");
				hidePop();
			}
		}
	});
}





//打开修改框
function updateFamilyPre(t){
	$.ajax({ 
		type : "get",
		dataType: "html",
		url : "../family/updateHealthFamilyPre",
		async: true,
		data:{
			healthFamilyId:$(t).attr("data-id"),
			sn:$(t).attr("data-sn")
			
		},
		success : function(data) {
			$("#healthdialog").empty().html(data);
			deleteM(t,null);
		}
	});
}
//执行修改动作
function updateFamily(){
	var memberId=$("#memberId").val();
	var healthFamilyId=$("#healthFamilyId").val();
	var diseaseName=$("#diseaseName").val();
	var relation=$("#relation").val();
	var remark=$("#remarkFamily").val();
	var lastUpdateNo=$("#lastUpdateNo").val();
	if(!validateLength(relation,"关系",1,16)){
		return;
	}
	if(!validateLength(diseaseName,"疾病名称",1,16)){
		return;
	}
	
	if(remark){
		if($.trim(remark).length > 120){
			zhyAlert("【备注】应少于120个字符！");
			return;
		}
		if(!interceptIllegal(remark)){
			zhyAlert("【备注】含有非法字符");
			return;
		}
	}
	var sn=$("#updateSn").val();//序号
	var trId='healthFamily_'+sn;
	$.ajax({
		type : "post",
		dataType: "json",
		url : "../family/updateHealthFamily",
		async: true,
		data:{
			healthFamilyId:healthFamilyId,
			diseaseName:diseaseName,
			relation:relation,
			lastUpdateNo:lastUpdateNo,
			remark:remark
		},
		success : function(data) {
			if(data.isSuccess){
				var heal_edit2 = data.heal_edit;//修改标示
				var heal_delete2 = data.heal_delete;//删除标示
				lastUpdateNo=eval(lastUpdateNo)+1;
				updateSuccessFamily(trId,sn,relation,diseaseName,healthFamilyId,lastUpdateNo,remark,heal_edit2,heal_delete2);
				hidePop();
				
			}else{
				zhyAlert("保存失败");
				hidePop();
			}
		}
	});
}
//删除
function removeFamily(t){
	var iconFirm=window.confirm("确定删除这条记录？");
	var sn=$(t).attr("data-sn");
	var historyType=$(t).attr("data-type");
	var dataSize
	var trId;
	
	var healthHistoryId=$(t).attr("data-id");
	var lastUpdateNo=$(t).attr("data-lastUpdateNo");
	
	var sizeOfList=$("#sizeOfListFamily").val();
	
	
	if(iconFirm==true){
		$.ajax({
			type : "post",
			dataType: "json",
			url : "../family/deleteHealthFamily",
			async: true,
			data:{
				healthFamilyId:healthHistoryId,
				lastUpdateNo:lastUpdateNo
			},
			success : function(data) {
				if(data.isSuccess){
					sizeOfList=(eval(sizeOfList)-1);
					removerTrFamily(t);
					hidePop();
					
				}else{
					zhyAlert("删除失败");
					hidePop();
				}
			}
		});
		
	}
	
}

function removerTrFamily(obj){
	
	obj =$(obj);
	var index =obj.parents("tr").find("td").eq(0);
	var next_tr=obj.parents("tr").nextAll();
	var trLenth=obj.parents("tbody").find("tr").length;
	if(trLenth==1){
		obj.parents("table").append("<tr id='NoDataFamily'><td style='text-align: center' colspan='5'>暂无数据</td></tr>");
	}
	next_tr.each(function(){
		var td=$(this).find("td").eq(0);
		td.text(td.text()-1);
	})
	obj.parents("tr").remove();
}






//修改成功调用
function updateSuccessFamily(trId,sn,relation,diseaseName,healthFamilyId,lastUpdateNo,remark,heal_edit,heal_delete){
	var updateTr=$("#"+trId);
//	var edit_str2 = "";
//	if(heal_edit){
//		edit_str2 ="<span class='colorblue edit'><a  " +
//		"data-id='"+healthFamilyId+"' data-sn='"+sn+"' " +
//		"data-lastUpdateNo='"+lastUpdateNo+"' onclick='updateFamilyPre(this)' " +
//		"href='javascript:;' class='colorblue'>修改</a></span>";
//	}
//	var del_str2 = "";
//	if(heal_delete){
//		del_str2= "<span data-id='"+healthFamilyId+"' data-lastUpdateNo='"+lastUpdateNo+"' " +
//		"  class='colorred delete' onclick='removeFamily(this)'>删除</span>";
//	}
	var edit_str2 = "";
		edit_str2 ="<span class='colorblue edit'><a  " +
						"data-id='"+healthFamilyId+"' data-sn='"+sn+"' " +
						"data-lastUpdateNo='"+lastUpdateNo+"' onclick='updateFamilyPre(this)' " +
						"href='javascript:;' class='colorblue'>修改</a></span>";
	var del_str2 = "";
		del_str2= "<span data-id='"+healthFamilyId+"' data-lastUpdateNo='"+lastUpdateNo+"' " +
								"  class='colorred delete' onclick='removeFamily(this)'>删除</span>";
	var newTr="<td>"+sn+"</td>"+
				"<td title='"+relation+"'>"+relation.substring(0,8)+(relation.length>8?"...":"")+"</td>"+
				"<td title='"+diseaseName+"'>"+diseaseName.substring(0,8)+(diseaseName.length>8?"...":"")+"</td>"+
				"<td title='"+remark+"'>"+remark.substring(0,16)+(remark.length>16?"...":"")+"</td>"+
				"<td>" + edit_str2 + del_str2 + "</td>";
	updateTr.empty();
	updateTr.append(newTr);
	
}
function addRowFamily(sizeOfList,diseaseName,healthFamilyId,relation,remark,heal_edit,heal_delete){//remark
	var iTable;
	var trId;
	iTable=$("#familyTable");
	trId='healthFamily_';
	$("#NoDataFamily").remove();
	$("#sizeOfListIll").val(sizeOfList);
	
//	var edit_str1 = "";
//	if(heal_edit){
//		edit_str1 ="<span class='colorblue edit'><a data-id='"+healthFamilyId+"' data-sn='"+sizeOfList+"' data-lastUpdateNo='1' onclick='updateFamilyPre(this)' " +
//		"href='javascript:;' class='colorblue'>修改</a> </span>";
//	}
//	var del_str1 = "";
//	if(heal_delete){
//		del_str1 = "<span data-id='"+healthFamilyId+"' data-lastUpdateNo='1' " +
//		"  class='colorred delete' onclick='removeFamily(this)'>删除</span>";
//	}
	var edit_str1 = "";
		edit_str1 ="<span class='colorblue edit'><a data-id='"+healthFamilyId+"' data-sn='"+sizeOfList+"' data-lastUpdateNo='1' onclick='updateFamilyPre(this)' " +
			"href='javascript:;' class='colorblue'>修改</a> </span>";
	var del_str1 = "";
		del_str1 = "<span data-id='"+healthFamilyId+"' data-lastUpdateNo='1' " +
					"  class='colorred delete' onclick='removeFamily(this)'>删除</span>";
	iTable.append("<tr id="+trId+sizeOfList+">" +
			"<td>"+sizeOfList+"</td>"+
			"<td title='"+relation+"'>"+relation.substring(0,8)+(relation.length>8?"...":"")+"</td>"+
			"<td title='"+diseaseName+"'>"+diseaseName.substring(0,8)+(diseaseName.length>8?"...":"")+"</td>"+
			"<td title='"+remark+"'>"+remark.substring(0,16)+(remark.length>16?"...":"")+"</td>"+
			"<td> "+ edit_str1 + del_str1 + "</td></tr>");
}



